//
//  StringTest.m
//  MacTestProject
//
//  Created by LiuLichen on 2022/4/11.
//

#import "StringTest.h"

@implementation StringTest

- (id) init {
    self = [super init];
    
    [self testInit];
    [self testConvert];
    [self testOperation];

    return self;
}

- (void) testInit {
    {
        int year = 8;
        NSString* s0 = @"Welcome to itcast";
        NSString* s1 = [NSString string];
        NSString* s2 = [[NSString alloc] initWithFormat: @"itcast has been %d year of history", year];
        NSString* s3 = [NSString stringWithFormat: @"itcast has been %d year of history", year];
        NSLog(@"s0: %@", s0);
        NSLog(@"s1: %@", s1);
        NSLog(@"s2: %@", s2);
        NSLog(@"s3: %@", s3);
    }
}

- (void) testConvert {
    {
        // c -> oc
        NSString* s0 = [[NSString alloc] initWithUTF8String: "jack"];
        NSLog(@"s0: %@", s0);
        // oc -> c
        const char* cs = [s0 UTF8String];
        NSLog(@"cs: %s", cs);
    }
}

- (void) testOperation {
    {
        NSString* str = @"itcast";
        NSUInteger strCount = [str length];
        NSLog(@"The length of str is %lu", strCount);
        for (int i = 0; i < strCount; i++) {
            char c = [str characterAtIndex: i];
            NSLog(@"The charactor at %d is %c", i, c);
        }
    }
    {
        NSString* str = @"WELCOME to itcast";
        NSLog(@"uppercase: %@", [str uppercaseString]);
        NSLog(@"lowercaseString: %@", [str lowercaseString]);
        NSLog(@"capitalizedString: %@", [str capitalizedString]);
    }
    {
        NSString* str1 = @"string itcast";
        NSString* str2 = @"str itcast";
        BOOL result = [str1 isEqualToString: str2];
        NSLog(@"%d", result);
        result = [str1 hasPrefix: @"string"];
        NSLog(@"%d", result);
        result = [str1 hasSuffix: @"itcast"];
        NSLog(@"%d", result);
    }
    {
        NSString* s1 = @"WELCOME to itcast";
        NSString* s2 = @"welcome to itcast";
        BOOL result = [s1 caseInsensitiveCompare: s2];
        NSLog(@"%d", result);
        result = [s1 compare: s2];
        NSLog(@"%d", result);
    }
    {
        NSString* s1 = @"string itcast";
        NSString* s2 = @"string";
        NSString* s3 = @"itcast";
        NSRange range1 = [s1 rangeOfString: s2];
        NSUInteger location1 = range1.location;
        NSUInteger length1 = range1.length;
        if (location1 != NSNotFound) {
            NSLog(@"s2 start at %lu and length is %lu", location1, length1);
        } else {
            NSLog(@"cannot find s2");
        }
        NSRange range2 = [s1 rangeOfString: s3];
        NSUInteger location2 = range2.location;
        NSUInteger length2 = range2.length;
        if (location2 != NSNotFound) {
            NSLog(@"s3 start at %lu and length is %lu", location2, length2);
        } else {
            NSLog(@"s3 find string");
        }
    }
    {
        NSString* s1 = @"itcast is best";
        NSLog(@"s1 substringToIndex: %@", [s1 substringToIndex: 3]);
        NSLog(@"s1 substringFromIndex: %@", [s1 substringFromIndex: 3]);
        NSLog(@"s1 substringWithRange: %@", [s1 substringWithRange: NSMakeRange(1, 4)]);
    }
    {
        NSMutableString* mStr = [NSMutableString string];
        [mStr appendString:@"itcast"];
        [mStr appendFormat:@" has %d year of history", 8];
        NSLog(@"%@", mStr);
        [mStr deleteCharactersInRange: [mStr rangeOfString: @"has"]];
        NSLog(@"%@", mStr);
        [mStr insertString: @" insert " atIndex: 3];
        NSLog(@"%@", mStr);
        [mStr replaceCharactersInRange: [mStr rangeOfString: @"history"] withString: @"replaced"];
        NSLog(@"%@", mStr);
    }
    {
        NSString* foo = @"badger 123";
        NSString* bar = [NSString stringWithFormat: @"badger %i", 123];
        NSLog(@"%d", (foo == bar));                 // NO
        NSLog(@"%d", [foo isEqual: bar]);           // YES
        NSLog(@"%d", [foo isEqualToString: bar]);   // YES
    }
}

@end
